package com.yvon.service.demo.spring.context.support;

import com.yvon.service.demo.spring.beans.BeansException;
import com.yvon.service.demo.spring.beans.factory.ConfigurableListableBeanFactory;
import com.yvon.service.demo.spring.beans.factory.config.BeanFactoryPostProcessor;
import com.yvon.service.demo.spring.beans.factory.config.BeanPostProcessor;
import com.yvon.service.demo.spring.context.ConfigurableApplicationContext;
import com.yvon.service.demo.spring.core.io.DefaultResourceLoader;

import java.util.Map;

/**
 * 应用程序上下文抽象类
 * 继承DefaultResourceLoader是为了处理spring.xml配置文件中配置资源的加载
 *
 * @author : Yvon
 * @since : 2022-12-06
 */
public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext {

    /**
     * 刷新容器
     */
    @Override
    public void refresh() throws BeansException {
        // 1. 创建 BeanFactory，并加载 BeanDefinition
        refreshBeanFactory();

        // 2. 获取 BeanFactory
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();

        // 3. 添加ApplicationContextAwareProcess类，让继承自ApplicationContextAware接口的Bean对象都能感知所属的ApplicationContext
        beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

        // 4. 在将Bean对象实例化之前，执行BeanFactoryPostProcess操作
        invokeBeanFactoryPostProcessors(beanFactory);

        // 5. BeanPostProcessor 需要在将Bean对象实例化之前注册
        registerBeanPostProcessors(beanFactory);

        // 6. 提前实例化单例Bean对象
        beanFactory.preInstantiateSingletons();
    }

    /**
     * 创建 BeanFactory，并加载 BeanDefinition
     */
    protected abstract void refreshBeanFactory() throws BeansException;

    /**
     * 获取 BeanFactory
     *
     * @return {@code ConfigurableListableBeanFactory }
     */
    protected abstract ConfigurableListableBeanFactory getBeanFactory();


    /**
     * 执行BeanFactoryPostProcess操作
     *
     * @param beanFactory bean工厂
     */
    private void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        // 从BeanDefinitionMap里取出所有的BeanFactoryPostProcessor，进行执行
        Map<String, BeanFactoryPostProcessor> beanFactoryPostProcessorMap = beanFactory.getBeansOfType(BeanFactoryPostProcessor.class);
        for (BeanFactoryPostProcessor beanFactoryPostProcessor : beanFactoryPostProcessorMap.values()) {
            beanFactoryPostProcessor.postProcessBeanFactory(beanFactory);
        }
    }

    /**
     * 注册Bean后处理器
     *
     * @param beanFactory bean工厂
     */
    private void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        // 从BeanDefinitionMap里取出所有的BeanFactoryPostProcessor, 添加到beanFactory里
        Map<String, BeanPostProcessor> beanPostProcessorMap = beanFactory.getBeansOfType(BeanPostProcessor.class);
        for (BeanPostProcessor beanPostProcessor : beanPostProcessorMap.values()) {
            beanFactory.addBeanPostProcessor(beanPostProcessor);
        }
    }

    @Override
    public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
        return getBeanFactory().getBeansOfType(type);
    }

    @Override
    public String[] getBeanDefinitionNames() {
        return getBeanFactory().getBeanDefinitionNames();
    }

    @Override
    public Object getBean(String name) throws BeansException {
        return getBeanFactory().getBean(name);
    }

    @Override
    public Object getBean(String name, Object... args) throws BeansException {
        return getBeanFactory().getBean(name, args);
    }

    @Override
    public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
        return getBeanFactory().getBean(name, requiredType);
    }

    @Override
    public void registerShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread(this::close));
    }

    @Override
    public void close() {
        getBeanFactory().destroySingletons();
    }


}
